/**
 * @class 代理父类
 * @author DuskyHuang 圣子
 * @description 隔离数据逻辑与系统（M）
*/
import { notifier, bridge, proxies } from '../boot';

export default abstract class Proxy {

    /**
      * 是否被初始化过
      * @return 判断结果
     */
    public get inited(): boolean { return this._inited; }

    protected constructor() {
        this._inited = true;
        proxies.unshiftUnique(this);
        this.init();
    }

    protected _inited: boolean;
    protected init(): void { }
    public update(...arg: any[]): void { }

    protected on(type: any, handler: Handler, priority: number = 0): void {
        notifier.attach(type, handler, this, false, priority);
    }

    protected once(type: any, handler: Handler, priority: number = 0): void {
        notifier.attach(type, handler, this, true, priority);
    }

    protected off(type: any, handler: Handler): void {
        notifier.detach(type, handler, this);
    }

    protected notify(type: any, ...args: any[]): void {
        notifier.dispatch(type, ...args);
    }

    public destroy(): void {
        notifier.unload(this);
        bridge.unload(this);
        proxies.remove(this);
    }
}